package com.wandian.game.sound
{
	import flash.events.*;
	import flash.media.*;

	public class SoundChannelEx extends EventDispatcher
	{
		private var fPlayingStopped_bl:Boolean;
		private var fPlayingCompleted_bl:Boolean;
		private var fInterruptionInProgress_bl:Boolean;
		private var fPlayingInterrupted_bl:Boolean;
		private var fSeamlessStopInProgress_bl:Boolean;
		private var fSoundChanel_sch:SoundChannel;
		private var fSoundTransformEx:SoundTransformEx;
		public static const EVENT_SOUND_CHANNEL_PLAYING_COMPLETED:String="onSoundChannelPlayingCompleted";
		public static const EVENT_SEAMLESS_VOLUME_CHANGING_STARTED:String="onSeamlessVolumeChangingStarted";
		public static const EVENT_SEAMLESS_STOP_STARTED:String="onSeamlessStopStarted";
		public static const EVENT_VOLUME_UPDATED:String="onVolumeValueUpdated";
		public static const EVENT_SOUND_CHANNEL_PLAYING_INTERRUPTED:String="onSoundChannelPlayingInterrupted";

		public function SoundChannelEx(param1:SoundChannel)
		{
			this.fSoundChanel_sch=param1;
			this.fSoundChanel_sch.addEventListener(Event.SOUND_COMPLETE, this.onSoundChanelPlayingCompleted, false, 1);
			this.fSoundTransformEx=new SoundTransformEx(this.fSoundChanel_sch.soundTransform);
			this.fSoundTransformEx.addEventListener(SoundTransformEx.EVENT_VOLUME_UPDATED, this.onVolumeUpdated);
			this.fSoundTransformEx.addEventListener(SoundTransformEx.EVENT_SEAMLESS_VOLUME_CHANGING_STARTED, this.onSeamlessVolumeChangingStarted);
			this.updateState(false, false, false, false, false);
			return;
		} // end function

		public function interruptPlaying(param1:Number=0):void
		{
			this.updateState(this.stopInProgress, true, this.playingStopped, false, this.playingCompleted);
			this.fSoundChanel_sch.removeEventListener(Event.SOUND_COMPLETE, this.onSoundChanelPlayingCompleted);
			this.fSoundTransformEx.resetAllDefferedVolumeChanges();
			this.setVolume(0, param1);
			if (param1 > 0)
			{
				this.fSoundTransformEx.addEventListener(SoundTransformEx.EVENT_SEAMLESS_VOLUME_CHANGING_COMPLETED, this.onInterruptionCompleted);
			}
			else
			{
				this.onInterruptionCompleted();
			}
			return;
		} // end function

		public function get playingInterrupted():Boolean
		{
			return this.fPlayingInterrupted_bl;
		} // end function

		public function get interruptionInProgress():Boolean
		{
			return this.fInterruptionInProgress_bl;
		} // end function

		public function get stopInProgress():Boolean
		{
			return this.fSeamlessStopInProgress_bl;
		} // end function

		public function setVolume(param1:Number, param2:Number=0, param3:Number=0, param4:Function=null):void
		{
			this.fSoundTransformEx.setVolume(param1, param2, param3, param4);
			return;
		} // end function

		public function get soundOn():Boolean
		{
			return this.fSoundTransformEx.soundOn;
		} // end function

		private function updateState(param1:Boolean, param2:Boolean, param3:Boolean, param4:Boolean, param5:Boolean):void
		{
			this.fSeamlessStopInProgress_bl=param1;
			this.fInterruptionInProgress_bl=param2;
			this.fPlayingInterrupted_bl=param4;
			this.fPlayingStopped_bl=param3;
			this.fPlayingCompleted_bl=param5;
			return;
		} // end function

		public function get soundChannel():SoundChannel
		{
			return this.fSoundChanel_sch;
		} // end function

		private function onVolumeUpdated(event:Event):void
		{
			this.fSoundChanel_sch.soundTransform=fSoundTransformEx.soundTransform;
			this.dispatchEvent(new Event(EVENT_VOLUME_UPDATED));
			return;
		} // end function

		public function set soundOn(param1:Boolean):void
		{
			this.fSoundTransformEx.soundOn=param1;
			return;
		} // end function

		private function onSeamlessVolumeChangingStarted(event:Event):void
		{
			this.dispatchEvent(new Event(EVENT_SEAMLESS_VOLUME_CHANGING_STARTED));
			return;
		} // end function

		private function onSoundChanelPlayingCompleted(event:Event):void
		{
			this.updateState(this.stopInProgress, this.interruptionInProgress, this.playingStopped, this.playingInterrupted, true);
			this.dispatchEvent(new Event(EVENT_SOUND_CHANNEL_PLAYING_COMPLETED));
			return;
		} // end function

		private function onInterruptionCompleted(event:Event=null):void
		{
			this.updateState(this.stopInProgress, false, this.playingStopped, true, this.playingCompleted);
			this.dispatchEvent(new Event(EVENT_SOUND_CHANNEL_PLAYING_INTERRUPTED));
			return;
		} // end function

		private function onSeamlessStopStarted(param1:Object):void
		{
			this.updateState(true, this.interruptionInProgress, this.playingStopped, this.playingInterrupted, this.playingCompleted);
			this.dispatchEvent(new Event(EVENT_SEAMLESS_STOP_STARTED));
			return;
		} // end function

		public function get playingCompleted():Boolean
		{
			return this.fPlayingCompleted_bl;
		} // end function

		public function stopToPlay(param1:Number=0, param2:Number=0):void
		{
			this.setVolume(0, param1, param2, this.onSeamlessStopStarted);
			return;
		} // end function

		public function get playingStopped():Boolean
		{
			return this.fPlayingStopped_bl;
		} // end function

	}
}
